package cn.dxh.stagefinal.utils;

import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.bean.Provider;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * IntelliJ IDEA -> stagefinal
 * 2019/12/12 15:10
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
//导出数据
public class EmployeeExcelUtils {
    //1.先创建一个Excel文件
    static HSSFWorkbook workbook;
    //2.创建文件中的sheet表   参数是sheet 名称
    static HSSFSheet sheet;
    //3.创建sheet中的行列信息
    //3.1创建第一行
    public static void  createFirst(String headname){
        workbook = new HSSFWorkbook();
         sheet = workbook.createSheet(headname);
        HSSFRow row = sheet.createRow(0);
        String[] heads ={"员工账号","姓名","手机号","所属部门","所属职位","最后登录时间","是否启动"};
        for (int i = 0; i < heads.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(heads[i]);
        }
    }

    //3.2创建除第一行以外的其他行，参数表示的被导出的数据的集合
    public static void createOthers(List<Employee>  list){
        for (int i = 0; i < list.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            Employee employee = list.get(i);
            HSSFCell cella = row.createCell(0);
            cella.setCellValue(employee.getLoginName());

            HSSFCell cellb = row.createCell(1);
            cellb.setCellValue(employee.geteName());

            HSSFCell cellc = row.createCell(2);
            cellc.setCellValue(employee.geteTel());

            HSSFCell celld = row.createCell(3);
            celld.setCellValue(employee.getDepartment().getdName());

            HSSFCell celle= row.createCell(4);
            celle.setCellValue(employee.getRole().getrName());

            HSSFCell cellf= row.createCell(5);
            cellf.setCellValue(employee.geteEndTime());

            HSSFCell cellg= row.createCell(6);
            if ( employee.geteState() == 0) {
                cellg.setCellValue("禁用");
            }
            else  {
                cellg.setCellValue("启用");
            }
        }
    }
//4.设置导出信息
    public static void export(OutputStream outputStream) throws IOException {
        //设置输出的方式是以网格的方式输出
        sheet.setGridsPrinted(true);
        //将文件写入到输出流中
        workbook.write(outputStream);
    }

}
